﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GDCal.dao;
using GDCal.vo;
using System.Collections;
using ESRI.ArcGIS.Geometry;

namespace GDCal
{
    public partial class AreaTypeForm : Form
    {
        public AreaTypeForm()
        {
            InitializeComponent();

            //Load all data and set to list view
            fetchDataToListView(new AreaTypeVO());
            this.delete.Enabled = false;
        }

        private void fetchDataToListView(AreaTypeVO filter)
        {
            ArrayList datas = AreaTypeDao.filter(filter);
            ListViewItem[] ret = new ListViewItem[datas.Count];

            int i = 0;
            foreach (AreaTypeVO data in datas)
            {
                ListViewItem listViewItem = new ListViewItem(new string[] { Convert.ToString(data.getId()), data.getType(), Convert.ToString(data.getAreaGroup()), Convert.ToString(data.getIsDefault()), data.getDescription() }, -1);
                ret[i++] = listViewItem;
            }

            this.listView.Items.Clear();
            this.listView.Items.AddRange(ret);
        }

        private void cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void addNew_Click(object sender, EventArgs e)
        {
            AreaTypeVO defaultVal = new AreaTypeVO();
            defaultVal.setAreaGroup(1);
            AreaTypeDetailForm areaTypeDetailForm = new AreaTypeDetailForm(defaultVal);
            areaTypeDetailForm.ShowDialog();

            if (areaTypeDetailForm.getAreaTypeVO().getId() > 0)
            {
                fetchDataToListView(new AreaTypeVO());
            }

            if (this.listView.SelectedItems.Count == 0)
                this.delete.Enabled = false;
        }

        private void listView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.listView.SelectedItems.Count > 0)
            {
                ListViewItem selectedItem = this.listView.SelectedItems[0];
                AreaTypeVO selectedVO = AreaTypeDao.getAreaType(Convert.ToInt32(selectedItem.SubItems[0].Text));
                AreaTypeDetailForm areaTypeDetailForm = new AreaTypeDetailForm(selectedVO);
                areaTypeDetailForm.ShowDialog();

                if (areaTypeDetailForm.getAreaTypeVO().getId() > 0)
                {
                    fetchDataToListView(new AreaTypeVO());
                }

                if (this.listView.SelectedItems.Count == 0)
                    this.delete.Enabled = false;
            }
        }

        private void delete_Click(object sender, EventArgs e)
        {
            if (this.listView.SelectedItems.Count > 0)
            {
                ListViewItem selectedItem = this.listView.SelectedItems[0];
                /*LandPriceVO landPriceFilter = new LandPriceVO();
                landPriceFilter.setStreetTypeId(Convert.ToInt32(selectedItem.SubItems[0].Text));
                ArrayList refVOs = LandPriceDao.filter(landPriceFilter);
                if (refVOs != null && refVOs.Count > 0)
                {
                    MessageBox.Show(GDCalMessages.Notify_StreetTypeRefInLandPriceDeleteError, GDCalMessages.Notify_Caption);
                }
                else
                {
                    AreaTypeDao.delAreaType(Convert.ToInt32(selectedItem.SubItems[0].Text));
                    fetchDataToListView(new AreaTypeVO());
                }*/
            }
        }

        private void listView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listView.SelectedItems.Count == 0)
                this.delete.Enabled = false;
            else
                this.delete.Enabled = true;
        }
    }
}